using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;


namespace ShapeEditor
{
    public partial class OutlineControl : UserControl
    {
        private BoxOutlineProxy boxOutlineProxy;

        private AssetManager assetMan;
       

        private delegate void OutlineHandler(BoxOutlineProxy bop);

        public OutlineControl()
        {
            InitializeComponent();
            assetMan = AssetManager.Instance;
            assetManagerDropDownControl1.Init(typeof(IShape));
            assetManagerDropDownControl1.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl1_SelectedEvent);
        }

        #region Public Properties
        //
        // Public properties
        //

       
        public BoxOutlineProxy BoxOutline
        {
            get { return boxOutlineProxy; }
        }
        //
        // Set the focus of this control to a new BoxOutline.  Be careful of thread issues
        private void SetBoxOutline_thisThread(BoxOutlineProxy BoxOutline)
        {
            boxOutlineProxy = null;
            //
            // Toggle Enabled based on whether the SlicePlane is null or not
            //
            if (BoxOutline == null)
            {
                this.Enabled = false;
            }
            else
            {
                this.Enabled = true;
                SharedBox box = BoxOutline.Box as SharedBox;
                assetManagerDropDownControl1.SelectItem(box);
                // Set check boxes
                if (BoxOutline.DrawBackEdges && BoxOutline.DrawFrontEdges)
                {
                    radioButton3.Checked = true;
                }
                else if (BoxOutline.DrawBackEdges)
                {
                    radioButton1.Checked = true;
                }
                else if(BoxOutline.DrawFrontEdges)
                {
                    radioButton2.Checked = true;
                }

            }
            boxOutlineProxy = BoxOutline;
        }
        //
        // Force the event to get called on the proper thread.  The DataGridView complains if you try
        //  to use it on the wrong thread.
        //
        public void SetBoxOutline(BoxOutlineProxy BoxOutline)
        {
            OutlineHandler newEvent = new OutlineHandler(SetBoxOutline_thisThread);
            object[] args = new object[1];
            args[0] = BoxOutline;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(newEvent, args);
                else
                    newEvent(BoxOutline);
            }
            catch { }
        }
        #endregion

        private void assetManagerDropDownControl1_SelectedEvent(IAssetManaged obj)
        {
            if (boxOutlineProxy != null)
            {
                // Assume for now only boxes
                boxOutlineProxy.Box = (SharedBox)obj;
            }
        }

        public IShape SelectShapeDefault()
        {
            List<IShape> iShapes = new List<IShape>();
            // Hack for now only SharedBox
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(SharedBox));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is IShape)
                    iShapes.Add((IShape)obj);
            }
            if (iShapes.Count > 0)
            {
                assetManagerDropDownControl1.SelectItem((IAssetManaged)iShapes[0]);
                return iShapes[0];
            }

            return null;
        }

        public delegate void BoxOutlineVisibility(bool isVisible);

        public void Visibility(bool isVisible)
        {
            BoxOutlineVisibility boxOutlineVisibility = new BoxOutlineVisibility(IsVisibleThisThread);

            object[] args = new object[1];
            args[0] = isVisible;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(boxOutlineVisibility, args);
                else
                    boxOutlineVisibility(isVisible);
            }
            catch { }

        }

        public void IsVisibleThisThread(bool isVisible)
        {
            this.Visible = isVisible;
        }


        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (boxOutlineProxy != null && radioButton1.Checked)
            {
                boxOutlineProxy.DrawBackEdges = true;
                boxOutlineProxy.DrawFrontEdges = false;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (boxOutlineProxy != null && radioButton2.Checked)
            {
                boxOutlineProxy.DrawFrontEdges = true;
                boxOutlineProxy.DrawBackEdges = false;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (boxOutlineProxy != null && radioButton3.Checked)
            {
                boxOutlineProxy.DrawBackEdges = true;
                boxOutlineProxy.DrawFrontEdges = true;
            }
        }
    }
}
